package fr.upmc.aladyn.test;

import fr.upmc.aladyn.transactionables.Transactionable;

/**
 * A class used for testing. Describes a Point using coordinates X and Y.
 * 
 * @author Ritchie Nithoo
 * @author Jules Fabry
 * @version 1.0
 * 
 */
@Transactionable
public class MyPoint {
	private int x;
	private int y;

	/**
	 * 
	 * @param x
	 *            The x coordinate
	 * @param y
	 *            The y coordinate
	 */
	public MyPoint(int x, int y) {
		this.x = x;
		this.y = y;
	}

	/**
	 * Default contructor. Sets the x and y coordinate to 0
	 */
	public MyPoint() {
		x = 0;
		y = 0;
	}

	/**
	 * Returns the x coordinate
	 * @return The x coordinate
	 */
	public int getX() {
		return x;
	}

	/**
	 * Returns the y coordinate
	 * @return The y coordinate
	 */
	public int getY() {
		return y;
	}

	/**
	 * Sets a new value of the x attribute
	 * @param newX
	 *            The new value of the x coordinate
	 */
	public void setX(int newX) {
		fr.upmc.aladyn.transactionables.BackUpManager.save(this);
		x = newX;
	}

	/**
	 * Sets a new value of the y attribute
	 * @param newY
	 *            The new value of the y coordinate
	 */
	public void setY(int newY) {
		fr.upmc.aladyn.transactionables.BackUpManager.save(this);
		y = newY;
	}

	/**
	 * This method throws a divided by zero exception
	 * 
	 * @param n
	 *            An integer that is added to the x coordinates
	 */
	@Transactionable
	public void add(int n) {
		try {
			fr.upmc.aladyn.transactionables.BackUpManager.begin();
			try {
				setX(x + n);
				setX(x + n);
				setX(x / 0);
				// MyPoint p = null;
				// p.getX();
			} catch (Exception e) {
				fr.upmc.aladyn.transactionables.BackUpManager.restore();
				System.out.println("l'exception");
			}
			fr.upmc.aladyn.transactionables.BackUpManager.end();
		} catch (Exception e) {
			fr.upmc.aladyn.transactionables.BackUpManager.restore();
			fr.upmc.aladyn.transactionables.BackUpManager.end();
			throw e;
		}
	}

	/**
	 * This method throws an exception and calls another method after the exception
	 * is caught.
	 * @param n An integer added to the x coordinate
	 */
	@Transactionable
	public void add2(int n) {
		try {
			fr.upmc.aladyn.transactionables.BackUpManager.begin();
			try {
				setX(x + n);
				setX(x + n);
				setX(x / 0);
			} catch (Exception e) {
				fr.upmc.aladyn.transactionables.BackUpManager.restore();
				System.out.println("l'exception");
			}
			setY(n);
			fr.upmc.aladyn.transactionables.BackUpManager.end();
		} catch (Exception e) {
			fr.upmc.aladyn.transactionables.BackUpManager.restore();
			fr.upmc.aladyn.transactionables.BackUpManager.end();
			throw e;
		}
	}

	/**
	 * A toString method to print the coordinates
	 */
	public String toString() {
		return " Point avec x=" + x + " et y=" + y;
	}
}
